<?php
namespace Entities\Corp;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @Entity(repositoryClass="\Entities\Corp\PessoaRepository")
 * @Table(name="corp_pessoa")
 * @HasLifecycleCallbacks
 * @InheritanceType("JOINED")
 * @DiscriminatorColumn(name="tp_pessoa", type="string", length=50)
 * @DiscriminatorMap({"pessoa" = "\Entities\Corp\Pessoa", "pessoafisica" = "\Entities\Corp\PessoaFisica", "pessoajuridica" = "\Entities\Corp\PessoaJuridica"})
 */
class Pessoa extends \Foxframe\Entity
{

	/**
	 * @Id
	 * @Column(type="integer")
	 * @GeneratedValue(strategy="AUTO")
	 */
	protected $id_pessoa;

	/**
	 * @Column(type="string", length=255)
	 */
	protected $ds_nome;

	/**
	 * @Column(type="string", length=1)
	 */
	protected $tp_genero;

	/**
	 * @Column(type="string", length=100)
	 */
	protected $ds_apelido;

	/**
	 * @Column(type="boolean")
	 */
	protected $fl_status		= true;

	/**
	 * @Column(type="boolean")
	 */
	protected $fl_usuario	= false;

	/**
	 * @Column(type="boolean")
	 */
	protected $fl_cliente	= false;

	/**
	 * @Column(type="boolean")
	 */
	protected $fl_fornecedor	= false;

	/**
	 * @Column(type="boolean")
	 */
	protected $fl_banco		= false;


	/**
	 * @OneToMany(targetEntity="\Entities\Corp\Telefone", mappedBy="pessoa", cascade={"all"}, fetch="LAZY")
	 */
	protected $telefones;

	/**
	 * @OneToMany(targetEntity="\Entities\Corp\Email", mappedBy="pessoa", cascade={"all"}, fetch="LAZY")
	 */
	protected $emails;

	/**
	 * @OneToMany(targetEntity="\Entities\Corp\Endereco", mappedBy="pessoa", cascade={"all"}, fetch="LAZY")
	 */
	protected $enderecos;

	/**
	 * @ManyToMany(targetEntity="\Entities\Corp\Perfil", mappedBy="usuarios", cascade={"all"}, fetch="LAZY")
	 */
	protected $perfis;

	public function __construct()
	{
		$this->telefones	= new ArrayCollection();
		$this->emails		= new ArrayCollection();
		$this->enderecos	= new ArrayCollection();
		$this->perfis		= new ArrayCollection();
	}

	public function __toString()
	{
		return $this->getDsApelido();
	}

	public function getId()
	{
		return $this->getIdPessoa();
	}

	public function getIdPessoa()
	{
		return $this->id_pessoa;
	}

	public function setFlStatus($value)
	{
		$this->fl_status = $value;
	}

	public function setFlUsuario($value)
	{
		$this->fl_usuario = $value;
	}

	public function setFlCliente($value)
	{
		$this->fl_cliente = $value;
	}

	public function setFlFornecedor($value)
	{
		$this->fl_fornecedor = $value;
	}

	public function setFlBanco($value)
	{
		$this->fl_banco = $value;
	}

	public function getDsApelido()
	{
		return $this->ds_apelido;
	}

	public function setDsApelido($value)
	{
		$this->ds_apelido = $value;
	}

	public function getFlStatus()
	{
		if ($this->fl_status)
			return 'Ativo';
		else
			return 'Inativo';
	}

	public function getFlUsuario()
	{
		if ($this->fl_usuario)
			return 'Sim';
		else
			return 'Não';
	}

	public function getFlCliente()
	{
		if ($this->fl_cliente)
			return 'Sim';
		else
			return 'Não';
	}

	public function getFlFornecedor()
	{
		if ($this->fl_fornecedor)
			return 'Sim';
		else
			return 'Não';
	}

	public function getDsNome()
	{
		return $this->ds_nome;
	}

	public function setDsNome($value)
	{
		$this->ds_nome = $value;
	}

	public function getTpPessoa()
	{
		if ($this instanceof \Entities\Corp\PessoaFisica)
			return 'Fisica';
		else if ($this instanceof \Entities\Corp\PessoaJuridica)
			return 'Juridica';
	}

	public function getTelefones()
	{
		return $this->telefones;
	}

	public function getEmails()
	{
		return $this->emails;
	}

	public function getCpfCnpj()
	{
		if ($this instanceof \Entities\Corp\PessoaFisica)
			return $this->getNuCpf();
		else if ($this instanceof \Entities\Corp\PessoaJuridica)
			return $this->getNuCnpj();
	}

	public function setTpGenero($value)
	{
		$this->tp_genero = strtoupper($value);
	}

	public function getTpGenero()
	{
		switch ($this->tp_genero)
		{
			case 'M':
				return 'Masculino';
				break;
			case 'F':
				return 'Feminino';
				break;
			default:
				return 'Indefinido';
				break;
		}
	}

	public function getPerfis()
	{
		return $this->perfis;
	}

	public function getGravatar($size = 40)
	{
		if (count($this->emails))
		{
			$title		= $this->getDsNome();
			$md5		= md5(strtolower(trim($this->emails[0]->getDsEmail())));
			$grav_url	= "http://www.gravatar.com/avatar/{$md5}?s={$size}";
			return "<img src=\"{$grav_url}\" title=\"{$title}\" alt=\"{$title}\" class='overTitle' >";
		}
	}
}